package com.springboot.husha.controller.app;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.husha.entity.Appuser;
import com.springboot.husha.entity.SysUser;
import com.springboot.husha.qpage.QPage;
import com.springboot.husha.satoken.StpUserUtil;
import com.springboot.husha.service.IAppuserService;
import com.springboot.husha.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-02-28
 */
@Controller
@RequestMapping("/api/front/appuser")
public class FrontAppuserController {


    /**
     * 路由地址
     */
    public String prefix = "/view/admin/appuser";

    /**
     * 业务层
     */
    @Autowired
    private IAppuserService service;

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    /**
     * 用户登录
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody Appuser obj) {
        if(obj.getAccount()==null){
            return Result.reError("账号不能为空");
        }
        if(obj.getPwd()==null){
            return Result.reError("密码不能为空");
        }
        QueryWrapper<Appuser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",obj.getAccount());
        queryWrapper.eq("pwd",obj.getPwd());
        Appuser appuser=this.service.getOne(queryWrapper);
        if(appuser==null){
            return Result.reError("账号密码错误");
        }
        StpUserUtil.login(appuser.getId());
        StpUserUtil.getSession().set(appuser.getId()+"",appuser);
        return Result.reOk("登录成功",StpUserUtil.getTokenValue());
    }


    /**
     * 用户注册
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestBody Appuser obj) {
        if(obj.getAccount()==null){
            return Result.reError("账号不能为空");
        }
        if(obj.getPwd()==null){
            return Result.reError("密码不能为空");
        }
        QueryWrapper<Appuser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",obj.getAccount());
        Appuser appuser=this.service.getOne(queryWrapper);
        if(appuser!=null){
            return Result.reError("账号已存在，请重新注册账号");
        }
        obj.setCtime(new Date());
        this.service.save(obj);
        return Result.reOk("注册成功");
    }



    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo() {
        if(!StpUserUtil.isLogin()){
            return Result.reTokenError("用户未登录");
        }
        return Result.reOk("查询成功", StpUserUtil.getSession().get(StpUserUtil.getLoginIdAsString()));
    }



}
